﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using RBAC.ORM;//请先添加引用
namespace RBAC.ORM.DAL
{
    /// <summary>
    /// 数据访问类SFunctionLib。
    /// </summary>
    public class SFunctionLib
    {
        public SFunctionLib()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string funID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from SFunctionLib");
            strSql.Append(" where funID=@funID ");
            SqlParameter[] parameters = {
					new SqlParameter("@funID", SqlDbType.NVarChar,50)};
            parameters[0].Value = funID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(RBAC.ORM.Model.SFunctionLib model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into SFunctionLib(");
            strSql.Append("funID,functionname,functiondescription)");
            strSql.Append(" values (");
            strSql.Append("@funID,@functionname,@functiondescription)");
            SqlParameter[] parameters = {
					new SqlParameter("@funID", SqlDbType.NVarChar,50),
					new SqlParameter("@functionname", SqlDbType.NVarChar,50),
					new SqlParameter("@functiondescription", SqlDbType.NVarChar,200)};
            parameters[0].Value = model.funID;
            parameters[1].Value = model.functionname;
            parameters[2].Value = model.functiondescription;

            try
            {
                DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(RBAC.ORM.Model.SFunctionLib model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update SFunctionLib set ");
            strSql.Append("functionname=@functionname,");
            strSql.Append("functiondescription=@functiondescription");
            strSql.Append(" where funID=@funID ");
            SqlParameter[] parameters = {
					new SqlParameter("@funID", SqlDbType.NVarChar,50),
					new SqlParameter("@functionname", SqlDbType.NVarChar,50),
					new SqlParameter("@functiondescription", SqlDbType.NVarChar,200)};
            parameters[0].Value = model.funID;
            parameters[1].Value = model.functionname;
            parameters[2].Value = model.functiondescription;

            try
            {
                DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string funID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from SFunctionLib ");
            strSql.Append(" where funID=@funID ");
            SqlParameter[] parameters = {
					new SqlParameter("@funID", SqlDbType.NVarChar,50)};
            parameters[0].Value = funID;

            try
            {
                DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public RBAC.ORM.Model.SFunctionLib GetModel(string funID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 funID,functionname,functiondescription from SFunctionLib ");
            strSql.Append(" where funID=@funID ");
            SqlParameter[] parameters = {
					new SqlParameter("@funID", SqlDbType.NVarChar,50)};
            parameters[0].Value = funID;

            RBAC.ORM.Model.SFunctionLib model = new RBAC.ORM.Model.SFunctionLib();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.funID = ds.Tables[0].Rows[0]["funID"].ToString();
                model.functionname = ds.Tables[0].Rows[0]["functionname"].ToString();
                model.functiondescription = ds.Tables[0].Rows[0]["functiondescription"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select funID,functionname,functiondescription ");
            strSql.Append(" FROM SFunctionLib ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" funID,functionname,functiondescription ");
            strSql.Append(" FROM SFunctionLib ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "SFunctionLib";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法
    }
}

